package controller.customer;

import java.sql.SQLException;
import java.util.ArrayList;

import hansot.to.CategoryTO;
import hansot.to.CommentTO;
import hansot.to.MemberTO;
import hansot.to.MenuTO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import service.hansot.MenuService;

@Controller
public class MenuController {
	private MenuService service;

	public MenuController(MenuService service) {
		this.service = service;
	}
	@RequestMapping("/menuView.do")
	public ModelAndView menuView(HttpServletRequest request,HttpServletResponse response,HttpSession session,String menuId) {
		ModelAndView mv = new ModelAndView();
		
		try {
			MenuTO muto = service.getMenuById(menuId);
			mv.addObject("muto",muto);
			ArrayList<CommentTO> commentList = service.getCommentByMenuId(menuId);
			mv.addObject("commentList",commentList);
			mv.setViewName("/view_menu");
		} catch (SQLException e) {
			mv = new ModelAndView("/error", "error_message",
					"이름으로 검색도중 오류발생 : " + e.getMessage());
		}
		
		return mv;
	}
	@RequestMapping("/foodMenu.do")
	public ModelAndView selectMenuByCategory(String categoryId){
		ModelAndView mv = null;
		try{
			ArrayList<MenuTO> list = service.selectMenuByCategory(categoryId);
			int count = list.size();
			HashMap map = new HashMap();
			map.put("list", list);
			map.put("count", count);
			mv = new ModelAndView("foodMenu/category","category",map);
		}catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("에러/error","error_message",e.getMessage());
		}
		return mv;
	}
	@RequestMapping("/orderPage.do")
	public ModelAndView orderPage(HttpServletRequest request,HttpServletResponse response,HttpSession session) {
		ModelAndView mv = null;
		MemberTO mto = (MemberTO)session.getAttribute("login_info");
		if(mto==null){
			mv = new ModelAndView("/error","error_message", "로그인 후 주문 할 수 있습니다. 로그인 먼저 하세요");
		}else{
			try{
				ArrayList<CategoryTO> list = service.selectCategory();
				HashMap map = new HashMap();
				map.put("category", list);
				map.put("num", list.size());
				mv = new ModelAndView("주문/orderPage","info",map);
			}catch(Exception e){
				e.printStackTrace();
				mv = new ModelAndView("에러/error","error_message",e.getMessage());
			}
		}
		return mv;
	}
	@RequestMapping("/getMenu.do")
	public ModelAndView getMenu(HttpServletRequest request,HttpServletResponse response, String categoryId) {
		ModelAndView mv = null;
		try{
			ArrayList<MenuTO> list = service.selectMenuByCategory(categoryId);
			JSONArray msg = new JSONArray(list);
//			System.out.println(array.toString());
//			String msg = array.toString();
//			mv.addObject("menu",list);
//			mv.setViewName("jsonView");
			mv = new ModelAndView("jsonView","menu",list);
		}catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("에러/error","error_message",e.getMessage());
		}
		return mv;
	}
}
